/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.paint.classic;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.paint.AbstractLettersDrawable;

public class WordBitmapDrawable extends AbstractLettersDrawable {
  private final int myColor;
  private final Paint myPaint = new Paint();
  private float myMinSize;

  private int myBitMapX;
  private int myBitMapY;
  private int myBitMapWidth;
  private int myBitMapHeight;

  private Bitmap myBitMap = null;
  
  private WordBattleView myWordBattleView;

  public WordBitmapDrawable( Word aWord, int aColor ) {
    super(aWord);
    myColor = aColor;

    init();
  }


  private void init(){
    myPaint.setColor( myColor );
    myPaint.setStyle(Style.FILL);
    myPaint.setAntiAlias(true);
  }

  private void detectSizes(){
    float theTopLeftX = Float.MAX_VALUE;
    float theBottomRightX = Float.MIN_VALUE;
    float theTopLeftY = Float.MAX_VALUE;
    float theBottomRightY = Float.MIN_VALUE;

    for(Letter theLetter : myWord.getLetters()){

      float theLetterTopLeftX = WordBattleView.BORDER_SIZE + theLetter.getColumn() * myWordBattleView.getWidthPerColumn();
      float theLetterBottomRightX = WordBattleView.BORDER_SIZE + (theLetter.getColumn()  + 1 )* myWordBattleView.getWidthPerColumn();
      float theLetterTopLeftY = WordBattleView.BORDER_SIZE + theLetter.getRow() * myWordBattleView.getHeightPerRow();
      float theLetterBottomRightY = WordBattleView.BORDER_SIZE + (theLetter.getRow() + 1) * myWordBattleView.getHeightPerRow();

      if(theLetterTopLeftX < theTopLeftX) theTopLeftX = theLetterTopLeftX;
      if(theLetterTopLeftY < theTopLeftY) theTopLeftY = theLetterTopLeftY;

      if(theLetterBottomRightX > theBottomRightX) theBottomRightX = theLetterBottomRightX;
      if(theLetterBottomRightY > theBottomRightY) theBottomRightY = theLetterBottomRightY;
    }

    myBitMapX = (int)(theTopLeftX - myWordBattleView.getWidthPerColumn());
    myBitMapY = (int)(theTopLeftY - myWordBattleView.getHeightPerRow());
    myBitMapWidth = (int)(theBottomRightX - theTopLeftX + 2 * myWordBattleView.getWidthPerColumn());
    myBitMapHeight = (int)(theBottomRightY - theTopLeftY + 2 * myWordBattleView.getHeightPerRow());
  }

  @Override
  public void paint( Canvas aCanvas, View aView ) {
    myWordBattleView = (WordBattleView)aView;
    
    myMinSize = (float)(Math.min(myWordBattleView.getHeightPerRow(), myWordBattleView.getWidthPerColumn()) * 0.7 );
    myPaint.setStrokeWidth( myMinSize );
    detectSizes();
    
    if(myWord.getEndPointLetters().isEmpty()) return;

    if(myBitMap == null){
      myPaint.setAlpha( 255 );
      myBitMap = Bitmap.createBitmap( (int)myBitMapWidth, (int)myBitMapHeight, Bitmap.Config.ALPHA_8 );
      Canvas theCanvas = new Canvas(myBitMap);

      Letter thePreviousLetter = null;
      float thePreviousCenterX = -1;
      float thePreviousCenterY = -1;

      //list of points that determine the line of selected letters
      //each line takes 4 points in the array x1,y1,x2,y2
      float[] thePoints = new float[(myWord.getEndPointLetters().size() - 1) * 4];
      for(int i=0;i<myWord.getEndPointLetters().size();i++){
        Letter theLetter = myWord.getEndPointLetters().get(i);

        //calculate the graphical center point of the letter we are currently processing
        //TODO this information is static we could calculate it once and cach it
        float theCenterX = (float)(WordBattleView.BORDER_SIZE + (theLetter.getColumn() + 0.5) * myWordBattleView.getWidthPerColumn());
        float theCenterY = (float)(WordBattleView.BORDER_SIZE + (theLetter.getRow() + 0.5) * myWordBattleView.getHeightPerRow());

        //draw a circle on each point where the line makes an angle, so this is on each endpoint except for the start and end point
        if(i > 0 && i < myWord.getEndPointLetters().size() - 1){
          theCanvas.drawCircle(theCenterX - myBitMapX, theCenterY - myBitMapY, myMinSize / 2, myPaint);
        }

        if(thePreviousLetter != null){
          //        theCanvas.drawLine(thePreviousCenterX, thePreviousCenterY, theCenterX, theCenterY, myPaint);
          int j = i - 1;
          thePoints[j*4] = thePreviousCenterX - myBitMapX;
          thePoints[j*4 + 1] = thePreviousCenterY - myBitMapY;
          thePoints[j*4 + 2] = theCenterX - myBitMapX;
          thePoints[j*4 + 3] = theCenterY - myBitMapY;
        }

//        if(thePreviousLetter != null){
//          theCanvas.drawLine(thePreviousCenterX - myBitMapX, thePreviousCenterY - myBitMapY, theCenterX - myBitMapX, theCenterY - myBitMapY, myPaint);
//        }

        thePreviousLetter = theLetter;
        thePreviousCenterX = theCenterX;
        thePreviousCenterY = theCenterY;
      }
      modifyFirstAndLastPoint(myWordBattleView, thePoints);
      theCanvas.drawLines(thePoints, myPaint);
    }

    myPaint.setAlpha( 180 );
    aCanvas.drawBitmap( myBitMap, myBitMapX, myBitMapY, myPaint );
  }
}
